#include "DAB.h"
#include "PSCAD_br_info.h"
void DAB::begin(param &Params)
{
    C0 = Params["C0"];
    C1 = Params["C1"];
    delt = Params["delt"];

    tr_params("L1") = Params["L1"];
    tr_params("L2") = Params["L2"];
    tr_params("Im") = Params["Im"];
    tr_params("Le") = Params["Le"];
    tr_params("Vtr_1") = Params["Vtr_1"];
    tr_params("Vtr_2") = Params["Vtr_2"];
    tr_params("Smva") = Params["Smva"];
    tr_params("freq") = Params["freq"];
    tr_params("delt") = Params["delt"];

    name = "DAB";

    brs.Vmap["REF"] = -1;
    brs.Vmap["N1"] = 0;
    brs.Vmap["N3"] = 1;
    brs.Vmap["N4"] = 2;
    brs.Vmap["T1"] = 3;
    brs.Vmap["T2"] = 4;
    brs.Vmap["T3"] = 5;
    brs.Vmap["T4"] = 6;

    brs.add_br("N1", "REF", "C1");
    brs.add_br("N3", "N4", "C2");

    brs.add_br("N1", "T1", "S1");
    brs.add_br("T1", "REF", "S2");
    brs.add_br("N1", "T2", "S3");
    brs.add_br("T2", "REF", "S4");
    brs.add_br("N3", "T3", "S5");
    brs.add_br("T3", "N4", "S6");
    brs.add_br("N3", "T4", "S7");
    brs.add_br("T4", "N4", "S8");

    brs.add_br("T1", "T2", "tr12");
    brs.add_br("T1", "T3", "tr13");
    brs.add_br("T1", "T4", "tr14");
    brs.add_br("T2", "T3", "tr23");
    brs.add_br("T2", "T4", "tr24");
    brs.add_br("T3", "T4", "tr34");

    A = brs.generate_A();
    num_node = brs.Vmap.size(); // 节点数含参考节点
    num_br = brs.size();
    Yg.resize(num_br);
    Yg.setZero();

    generate_map("./map/" + name + "_map.map");

    C[0].C = C0;
    C[0].delt = delt;
    C[0].begin();

    C[1].C = C1;
    C[1].delt = delt;
    C[1].begin();

    tr[0].delt = delt;
    tr[0].begin(tr_params);

    for (auto &&igbt : IGBT)
    {
        igbt.delt = delt;
        igbt.begin();
        igbt.simple = true;
    }
}

void DAB::setSig(int T1, int T2, int T3, int T4, int T5, int T6, int T7, int T8)
{
    sigs[0] = T1;
    sigs[1] = T2;
    sigs[2] = T3;
    sigs[3] = T4;

    sigs[4] = T5;
    sigs[5] = T6;
    sigs[6] = T7;
    sigs[7] = T8;
}

void DAB::control()
{
    for (int i = 0; i < 8; i++)
        IGBT[i].setSig(sigs[i]);
}

void DAB::dsdyn_out_iteration(double delt, double simulation_time)
{
    for (auto &&C_ : C)
        C_.dsdyn();
    for (auto &&igbt : IGBT)
        igbt.dsdyn_out_iteration(delt, simulation_time);
    tr[0].dsdyn(delt, simulation_time);

    bool flag = false;
    if (!flag)
    {
        flag = true;
        Yg.diagonal()[brs("C1")] = C[0].GC;
        Yg.diagonal()[brs("C2")] = C[1].GC;
        Yg.diagonal()[brs("tr12")] = tr->brs["12"].Gbr;
        Yg.diagonal()[brs("tr13")] = tr->brs["13"].Gbr;
        Yg.diagonal()[brs("tr14")] = tr->brs["14"].Gbr;
        Yg.diagonal()[brs("tr23")] = tr->brs["23"].Gbr;
        Yg.diagonal()[brs("tr24")] = tr->brs["24"].Gbr;
        Yg.diagonal()[brs("tr34")] = tr->brs["34"].Gbr;
    }
}

void DAB::dsdyn_in_iteration(double delt, double simulation_time)
{
    for (auto &&igbt : IGBT)
        igbt.dsdyn_in_iteration(delt, simulation_time);
}

void DAB::dsout_in_iteration()
{
}

void DAB::dsout_out_iteration()
{
    for (auto &&igbt : IGBT)
        igbt.dsout_out_iteration();

    // 所需的数值应该直接从电气网络中获取，而非依赖dsout的传入
    // 但是怎么建立映射关系？还是需要branch的绑定
    // 怎么随时同步两个branch的信息呢？以谁为主，以谁为辅？
    C[0].VC = V(brs.Vmap["Xe1"]) - V(brs.Vmap["REF"]);
    C[0].dsout();

    C[1].VC = V(brs.Vmap["Xe2"]) - V(brs.Vmap["Xe3"]);
    C[1].dsout();
}